<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Alejandro Casas
 * Date: 2/27/13
 * Time: 10:13 PM
 * To change this template use File | Settings | File Templates.
 */
session_start();
require 'facebook-php-sdk-master/src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
    'appId'  => '130496170462251',
    'secret' => '7ed6a5d09450be4e6214e35a022f8c8e',
));

// Get User ID
$_SESSION['fbuser'] = $facebook->getUser();

// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.

if ($_SESSION['fbuser']) {
    try {
// Proceed knowing you have a logged in user who's authenticated.
        $_SESSION['fbuser_profile'] = $facebook->api('/me');

    } catch (FacebookApiException $e) {
        error_log($e);
        $_SESSION['fbuser'] = null;
    }
}

// Login or logout url will be needed depending on current user state.
if ($_SESSION['fbuser']) {
    $logoutUrl = $facebook->getLogoutUrl();
} else {
//$permission= array('scope'=>'user_birthday,email','redirect_uri' => 'http://www.cluvie.com/fb.php');
    $params = array(
        'scope' => 'email,user_birthday,publish_actions,create_event,user_location',
        'redirect_uri' => 'http://www.sportsaround.us/fbsuccess.php',
    );
    $loginUrl = $facebook->getLoginUrl($params);
}

if ($_SESSION['fbuser'])
{
    $facebook->destroySession();
    ?>
    Redirecting...
    <script>
        location.replace("comingsoon.php");
    </script>
    <?php
}
else{
    $facebook->destroySession();
    ?>
Redirecting...
<script>
    location.replace("comingsoon.php");
</script>
<?php
}
?>